from flask import Blueprint,request,jsonify
from flask_restful import Api,Resource,reqparse
from models.users import Fees,Toos
from models.users import db

#创建蓝图
view = Blueprint('views', __name__,)

api = Api(view)



#添加商品
class Tian(Resource):
    def get(self):
        user = Toos.query.all()
        # 返回用户信息
        list=[]
        for i in user:
           list.append({
               'id': i.id,
               'name': i.name,
               'img': i.img,
               'goog': i.goog,
               'shi': i.shi,
               'dan': i.dan,
               'shu': i.shu,
               'xiao': i.xiao
           })
        return jsonify(list)

    def post(self):
        # name=request.form.get('name')
        # img=request.form.get('img')
        # goog=request.form.get('goog')
        # shi=request.form.get('shi')
        # dan=request.form.get('dan')
        # shu=request.form.get('shu')
        # xiao=request.form.get('xiao')

        res = reqparse.RequestParser()
        res.add_argument('name')
        data = res.parse_args()
        name = data['name']
        print('>>>:', name)

        res = reqparse.RequestParser()
        res.add_argument('img')
        data = res.parse_args()
        img= data['img']
        print('>>>:', img)

        res = reqparse.RequestParser()
        res.add_argument('goog')
        data = res.parse_args()
        goog = data['goog']
        print('>>>:', goog)

        res = reqparse.RequestParser()
        res.add_argument('shi')
        data = res.parse_args()
        shi= data['shi']
        print('>>>:',shi)

        res = reqparse.RequestParser()
        res.add_argument('dan')
        data = res.parse_args()
        dan = data['dan']
        print('>>>:', dan)

        res = reqparse.RequestParser()
        res.add_argument('shu')
        data = res.parse_args()
        shu = data['shu']
        print('>>>:', shu)

        res = reqparse.RequestParser()
        res.add_argument('xiao')
        data = res.parse_args()
        xiao= data['xiao']
        print('>>>:',xiao)
        if not all([name,img,goog,shi,dan,shu,xiao]):
            return jsonify({'code':400,'msg':'参数不完整'})
        user=Toos(name=name,img=img,shi=shi,goog=goog,dan=dan,shu=shu,xiao=xiao)
        # 添加
        db.session.add(user)
        # 提交事务
        db.session.commit()
        return jsonify({'code':200,'msg':'添加成功'})



# #修改商品
class Xou(Resource):
    # def put(self,id):
    #     # shu = request.form.get('shu')
    #     res = reqparse.RequestParser()
    #     res.add_argument('shu')
    #     data = res.parse_args()
    #     shu = data['shu']
    #     print('>>>:', shu)
    #     Toos.query.filter_by(id=id).update({'shu':shu})
    #     db.session.commit()
    #     return jsonify({
    #         'code': 200,
    #         'msg': '数量修改成功'
    #     })

    def put(self, id):
        user = Toos.query.filter_by(id=id).first()
        print('>>>>>>',user)
        if  int(user.shu)==0:
            return  jsonify({'code':400,'msg':"数量为0，不能在点击了"})
        user.shu= int(user.shu) -1

        db.session.commit()
        return jsonify({'code': 200, "msg": "修改成功"})



#修改商品
class Xt(Resource):
    def put(self, id):
      user = Toos.query.filter_by(id=id).first()
      user.shu = int(user.shu) + 1
      db.session.commit()
      return jsonify({'code': 200, "msg": "修改成功"})



# 删除商品
class Shan(Resource):
    def delete(self,id):
      message = Toos.query.get(id)
      print('>>>>>>>:',message)
      db.session.delete(message)
      db.session.commit()
      return jsonify({
          'code': 200,
          'msg': '删除成功'
      })







#详情表
class Users(Resource):
    def get(self):
        """获取一个用户信息"""
        # 获取查询字符串参数
        uid = request.args.get('id')
        print(">>>>>>>>>>>..",uid)
        # 从模型类中查询
        user = Fees.query.filter_by(fee_id=uid).first()
        print("ssssss..",user)
        # 返回用户信息
        list =[{
            'id':user.id,
            'name': user.name,
            'ss':user.fee_id
        }]

        return jsonify(list)
















#定义路由
api.add_resource(Tian,'/tian/')

# 删除商品
api.add_resource(Shan,'/shan/<int:id>/')

#修改商品
api.add_resource(Xou,'/xou/<int:id>/')

#修改商品
api.add_resource(Xt,'/xt/<int:id>/')

#详情表
api.add_resource(Users,'/users/')